package com.zhongjianjian.elasticjob.taskConfig;

import com.dangdang.ddframe.job.config.JobCoreConfiguration;
import com.dangdang.ddframe.job.config.simple.SimpleJobConfiguration;
import com.dangdang.ddframe.job.lite.api.JobScheduler;
import com.dangdang.ddframe.job.lite.config.LiteJobConfiguration;
import com.dangdang.ddframe.job.lite.spring.api.SpringJobScheduler;
import com.dangdang.ddframe.job.reg.zookeeper.ZookeeperConfiguration;
import com.dangdang.ddframe.job.reg.zookeeper.ZookeeperRegistryCenter;
import com.zhongjianjian.elasticjob.MyJobTask;
import com.zhongjianjian.elasticjob.utils.SpringContextUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * 作者: LCG
 * 日期: 2020/2/24 09:40
 * 描述:
 */
//@Configuration
public class MyElasticJobConfig {

    @Autowired
    private ZookeeprConfig zookeeprConfig;



    @Bean("jobCoreConfiguration")
    public JobCoreConfiguration getJobConfigure(){
        JobCoreConfiguration jobCoreConfiguration = JobCoreConfiguration.newBuilder("Test-Job", "0/1 * * * * ?", 4)
                .shardingItemParameters("0=T0,1=T1,2=T2,3=T3")//分片的参数配置
                .jobParameter("JOb的参数")
                .failover(true)//允许失效转移
                .description("Job的描述，介绍这个JOB是干什么的")
                .misfire(false)
                .build();
        return jobCoreConfiguration;
    }


    @Bean("simpleJobConfig")
    public SimpleJobConfiguration getsimpleJobType(JobCoreConfiguration jobCoreConfiguration){
        // 定义SIMPLE类型配置
        SimpleJobConfiguration simpleJobConfig = new SimpleJobConfiguration(jobCoreConfiguration, MyJobTask .class.getCanonicalName());

        return simpleJobConfig;
    }

    @Bean(value = "liteJobConfiguration")
    public LiteJobConfiguration getLiteJobConfigure(SimpleJobConfiguration simpleJobConfig){
        LiteJobConfiguration liteJobConfiguration = LiteJobConfiguration.newBuilder(simpleJobConfig).build();
        return liteJobConfiguration;
    }

    /**
     * 配置Zookeeper 信息
     * @param zookeeprConfig
     * @return
     */
    @Bean("zookeeperConfiguration")
    public ZookeeperConfiguration getZookeeperConfiguration(ZookeeprConfig zookeeprConfig){

        ZookeeperConfiguration zookeeperConfiguration= new ZookeeperConfiguration(zookeeprConfig.getLists(),zookeeprConfig.getNamespace());

        return zookeeperConfiguration;
    }

    /**
     * 配置zookeepr注册中心
     * @param zookeeperConfiguration
     * @return
     */
    @Bean(value = "zookeeperRegistryCenter",initMethod = "init")
    public ZookeeperRegistryCenter getZookeeperRegistryCenter(ZookeeperConfiguration zookeeperConfiguration){
        ZookeeperRegistryCenter zookeeperRegistryCenter = new ZookeeperRegistryCenter(zookeeperConfiguration);
        return zookeeperRegistryCenter;
    }

    /**
     * 配置JOb的启动
     * @param liteJobConfiguration
     * @param zookeeperRegistryCenter
     * @return
     */
    @Bean(initMethod = "init")
    public JobScheduler getJobScheduler(LiteJobConfiguration liteJobConfiguration,ZookeeperRegistryCenter zookeeperRegistryCenter){

        MyJobTask myJobTask = SpringContextUtils.getBean(MyJobTask.class);

        return new SpringJobScheduler(myJobTask,zookeeperRegistryCenter,liteJobConfiguration);

        //return new JobScheduler(zookeeperRegistryCenter, liteJobConfiguration);
    }

}
